<?php
Yii::import('application.components.U');
Yii::import("application.components.tbs_class_php5", true);
Yii::import("application.components.tbs_plugin_excel", true);
class ReportController extends GxController
{
    private $TBS;
    private $logo;
    private $format;
    public function init()
    {
        parent::init();
        if (!isset($_POST) || empty($_POST)){
                $this->redirect(url('/'));
        }
        $this->logo = bu() . "/images/logo.png";
        $this->TBS = new clsTinyButStrong;
        $this->format = $_POST['format'];
        if ($this->format == 'excel') {
            $this->TBS->PlugIn(TBS_INSTALL, TBS_EXCEL);
        }
        error_reporting(E_ERROR);
    }
    public function actionViewCustomerHistory()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $customer_id = $_POST['customer_id'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'newcustomers.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'view_customer_history.htm');
            }
            $cust = U::report_view_customer_history($customer_id);
            $customer = Customers::model()->findByPk($customer_id);
            $this->TBS->MergeBlock('header', array(array('logo' => $this->logo, 'customer' => $customer->nama_customer)));
            $this->TBS->MergeBlock('history', $cust);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "ViewCustomerHistory_$customer->nama_customer.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionNewCustomers()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'newcustomers.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'newcustomers.htm');
            }
            $cust = U::report_new_customers($from, $to);
            $this->TBS->MergeBlock('header', array(array('logo' => $this->logo, 'start' => $from, 'to' => $to)));
            $this->TBS->MergeBlock('cust', $cust);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "newcustomers$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionCashOut()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'biaya.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'biaya.htm');
            }
            $biaya = U::report_biaya($from, $to);
            $this->TBS->MergeBlock('header', array(array('logo'=>$this->logo,'start' => $from, 'to' => $to)));
            $this->TBS->MergeBlock('cash', $biaya);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "cashout$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionInventoryMovements()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'mutasi_stok.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'mutasi_stok.htm');
            }
            $mutasi = U::report_mutasi_stok($from, $to);
            $this->TBS->MergeBlock('header', array(array('logo'=>$this->logo,'start' => $from, 'to' => $to)));
            $this->TBS->MergeBlock('mutasi', $mutasi);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "InventoryMovements$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionInventoryCard()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            $barang_id = $_POST['barang_id'];
            $saldo_awal = StockMoves::get_saldo_item_before($barang_id, $from);
            $barang = Barang::model()->findByPk($barang_id);
            $row = U::report_kartu_stok($barang_id, $from, $to);
            $stock_card = array();
            foreach ($row as $newrow) {
                $newrow['before'] = $saldo_awal;
                $newrow['after'] += $newrow['before'] + $newrow['qty'];
                $saldo_awal = $newrow['after'];
                $stock_card[] = $newrow;
            }
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'kartu_stok.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'kartu_stok.htm');
            }
            $this->TBS->MergeBlock('header', array(array('logo'=>$this->logo,'start' => $from, 'to' => $to, 'item' => $barang->nama_barang)));
            $this->TBS->MergeBlock('mutasi', $stock_card);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "InventoryCard$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionBeautySummary()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'beauty_rekap.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'beauty_rekap.htm');
            }
            $summary = U::report_beauty_summary($from, $to);
            $this->TBS->MergeBlock('header', array(array('logo'=>$this->logo,'start' => $from, 'to' => $to)));
            $this->TBS->MergeBlock('beauty', $summary);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "BeautySummary$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionBeautyDetails()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'beauty_details.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'beauty_details.htm');
            }
            $summary = U::report_beauty_details($from, $to);
            $this->TBS->MergeBlock('header', array(array('logo'=>$this->logo,'start' => $from, 'to' => $to)));
            $this->TBS->MergeBlock('beauty', $summary);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "BeautyDetails$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionSalesSummary()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            $grup_id = $_POST['grup_id'];
            $grup_name = 'ALL';
            if ($grup_id != null) {
                $grup = Grup::model()->findByPk($grup_id);
                $grup_name = $grup->nama_grup;
            }
            $summary = U::report_sales_summary($grup_id, $from, $to);
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'sales_rekap.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'sales_rekap.htm');
            }
            $this->TBS->MergeBlock('header', array(array('logo' => $this->logo, 'start' => $from, 'to' => $to, 'grup' => $grup_name)));
            $this->TBS->MergeBlock('sales', $summary);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "SalesSummary$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
    public function actionSalesDetails()
    {
        if (Yii::app()->request->isAjaxRequest) return;
        if (isset($_POST) && !empty($_POST)) {
            $from = $_POST['tglfrom'];
            $to = $_POST['tglto'];
            $grup_id = $_POST['grup_id'];
            $grup_name = 'ALL';
            if ($grup_id != null) {
                $grup = Grup::model()->findByPk($grup_id);
                $grup_name = $grup->nama_grup;
            }
            $summary = U::report_sales_details($grup_id, $from, $to);
            if ($this->format == 'excel') {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'sales_details.xml');
            } else {
                $this->TBS->LoadTemplate(Yii::getPathOfAlias('application.views.reports') . DIRECTORY_SEPARATOR . 'sales_details.htm');
            }
            $this->TBS->MergeBlock('header', array(array('logo' => $this->logo, 'start' => $from, 'to' => $to, 'grup' => $grup_name)));
            $this->TBS->MergeBlock('sales', $summary);
            if ($this->format == 'excel') {
                $this->TBS->Show(TBS_EXCEL_DOWNLOAD, "SalesDetails$from-$to.xls");
            } else {
                $this->TBS->Show();
            }
        }
    }
}